<template>
<div>
</div>
</template>

<script>
import jwt_decode from "jwt-decode";
import {
    convertKey,
    deleteChildren
} from '@/library'
export default {
    name: "iDaaS",
    data() {
        return {
            user: {
                grant_type: "sso_token",
                id_token: ""
            }
        }
    },
    methods: {
        login(path = "Map") {
            localStorage.removeItem('userInfo');
            localStorage.removeItem('ShowMenu/setSideMenu');
            localStorage.removeItem('currentTask/setCurrentTask');
            this.$common
                .oauthLogin(this.user)
                .then((res) => {
                    if (res.access_token) {
                        let {
                            access_token,
                            userIdentity,
                            administrativeCode,
                            userLevel,
                            loginName,
                        } = res;
                        let jwt = jwt_decode(access_token);
                        let {
                            exp,
                            authorities
                        } = jwt;
                        let UserInfo = {
                            original: res,
                            token: access_token,
                            authorities: authorities,
                            header: exp,
                            userLevel: userLevel,
                            administrativeCode: administrativeCode,
                            loginName: loginName,
                            userName: loginName,
                            UserIdentity: userIdentity[0],
                        };
                        localStorage.setItem("userInfo", JSON.stringify(UserInfo));
                        this.$store.dispatch("User/setAsyncUserInfo", UserInfo);
                        this.$common.getAdministrativeList().then((res) => {
                            let options = JSON.parse(JSON.stringify(res.data.treeItem, ['administrativeName', 'administrativeCode', 'children']));
                            options = convertKey(options, {
                                'administrativeName': 'label',
                                'administrativeCode': 'value'
                            })
                            options = deleteChildren(options)
                            localStorage.setItem('getTreeData/setAsyncVillageData', JSON.stringify(options))
                            this.$store.dispatch('getTreeData/setAsyncVillageData')
                            console.log(options)
                        });
                    }
                    localStorage.setItem('SYS_TYPE', this.user.system)
                    this.$message({
                        type: "success",
                        message: "登录成功",
                        duration: 300,
                    });
                    setTimeout(() => {
                        this.$router.push({
                            name: path,
                        });
                    }, 0);
                })
                .catch((err) => {
                    this.$message({
                        type: "error",
                        message: '帐号或密码错误',
                        duration: 3000,
                    });
                });
        },
    },
    created() {
        let pos = location.href.indexOf('?')
        let geturl = new URLSearchParams(location.href.substring(pos + 1))
        if (geturl.has('id_token')) {
            this.user.id_token = geturl.get('id_token')
            if (this.$route.name === 'iDaasPlant') {
                this.user.system = "SS2"
            } else {
                this.user.system = "SS1"
            }
            this.login()
        }
    }
}
</script>
